﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using System.Data;

namespace Joyintech.Admin.inpiao365.general.system.menu
{
    public partial class edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            { 
                //绑定菜单（如果是修改菜单则需排除其自身及其下级菜单）
                DataBindSystemMenu();

                //判断是否为修改操作
                if (DNTRequest.GetQueryString("menu_id") != "")
                {
                    DataBindForm();
                }
            }
        }

        #region 绑定菜单（如果是修改菜单则需排除其自身及其下级菜单）

        /// <summary>
        /// 绑定所有菜单
        /// </summary>
        private void DataBindSystemMenu()
        {
            this.ddlMenuPID.Items.Clear();

            this.ddlMenuPID.Items.Add(new ListItem("作为一级菜单", "0"));

            List<systemMenu> list = new List<systemMenu>();

            if (DNTRequest.GetQueryString("menu_id") != "")
            {
                list = new systemMenu_BLL().GetModelList("menuPID=0 and menuID<>" + DNTRequest.GetQueryString("menu_id") + " order by menuOrder");
            }
            else
            {
                list = new systemMenu_BLL().GetModelList("menuPID=0");
            }

            foreach (systemMenu model in list)
            {
                this.ddlMenuPID.Items.Add(new ListItem(model.MenuName, model.MenuId.ToString()));

                //递归绑定下级菜单
                DataBindSystemMenuChild(model.MenuId, "--");
            }
        }

        /// <summary>
        /// 递归绑定下级菜单
        /// </summary>
        /// <param name="menuPID"></param>
        private void DataBindSystemMenuChild(int menuPID,string frontStr)
        {
            List<systemMenu> list = new List<systemMenu>();

            if (DNTRequest.GetQueryString("menu_id") != "")
            {
                list = new systemMenu_BLL().GetModelList("menuPID=" + menuPID + " and menuID<>" + DNTRequest.GetQueryString("menu_id") + " order by menuOrder");
            }
            else
            {
                list = new systemMenu_BLL().GetModelList("menuPID=0" + menuPID);
            }

            foreach (systemMenu model in list)
            {
                this.ddlMenuPID.Items.Add(new ListItem(frontStr + model.MenuName, model.MenuId.ToString()));

                //递归绑定下级菜单
                DataBindSystemMenuChild(model.MenuId, "--" + frontStr);
            }
        }

        #endregion

        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm()
        {
            DataSet ds = new DataSet();

            ds = new systemMenu_BLL().GetList("menuID=" + DNTRequest.GetQueryString("menu_id"));

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表systemMenu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            systemMenu model = new systemMenu();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("menu_id") != "")
            {
                model.MenuId = Convert.ToInt32(DNTRequest.GetQueryString("menu_id").ToString());

                dataResult = new systemMenu_BLL().Update(model);
            }
            else
            {
                dataResult = new systemMenu_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "parent.location.href=parent.location.href;");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");                
            }
        }

        #endregion
    }
}